﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VirtualTournaments.Business.Tournaments;

namespace VirtualTournaments.Business.Players
{
    public class Player
    {
        private const byte ABILITIES_NUMBER = 4;
        private const byte MAXIMUM_ABILITY = 100;
        private const byte MAXIMUM_ABILITIES_AVERAGE = 60;

        public enum Roles { GOALKEEPER, DEFENDER, MIDFIELDER, FORWARD };
        
        private Name _name;
        private Team _team;
        private Roles _role;
        private byte _speed;
        private byte _precision;
        private byte _strength;
        private byte _dribbling;

        public Player(Name name, Team team, Roles role, byte speed, byte precision, byte strength, byte dribbling)
        {
            Name = name;
            Team = team;
            Role = role;
            Speed = speed;
            Precision = precision;
            Strength = strength;
            Dribbling = dribbling;
        }

        public Name Name
        {
            get { return _name; }
            protected set { _name = value; }
        }

        public virtual Team Team
        {
            get { return _team; }
            protected set { _team = value; }
        }

        public Roles Role
        {
            get { return _role; }
            set { _role = value; }
        }

        public byte Speed
        {
            get { return _speed; }
            set 
            {
                byte currentSpeed = Speed;
                _speed = value;
                if (!CheckAbilities (value))
                {
                    _speed = currentSpeed;
                    throw new InvalidAbilityException();
                } 
            }
        }

        public byte Precision
        {
            get { return _precision; }
            set 
            { 
                byte currentPrecision = Speed;
                _precision = value;
                if (!CheckAbilities (value))
                {
                    _precision = currentPrecision;
                    throw new InvalidAbilityException();
                }
                _precision = value; 
            }
        }

        public byte Strength
        {
            get { return _strength; }
            set 
            { 
                byte currentStrength = Speed;
                _strength = value;
                if (!CheckAbilities (value))
                {
                    _strength = currentStrength;
                    throw new InvalidAbilityException();
                }
                _strength = value; 
            }
        }

        public byte Dribbling
        {
            get { return _dribbling; }
            set 
            { 
                byte currentDribbling = Dribbling;
                _dribbling = value;
                if (!CheckAbilities (value))
                {
                    _dribbling = currentDribbling;
                    throw new InvalidAbilityException();
                }
                _dribbling = value; 
            }
        }

        private bool CheckAbilities (byte ability)
        {
            if (ability > MAXIMUM_ABILITY)
                return false;
            if ((Speed + Precision + Strength + Dribbling) > ABILITIES_NUMBER*MAXIMUM_ABILITIES_AVERAGE)
                return false;
            return true;
        }

    }
}
